package com.sanhuo.idms.service;

import com.sanhuo.idms.bo.AdminUpdateStudentBO;
import com.sanhuo.idms.bo.StudentPageBO;
import com.sanhuo.idms.bo.StudentSaveBO;
import com.sanhuo.idms.dao.StudentMapper;
import com.sanhuo.idms.entity.*;
import com.sanhuo.idms.vo.StudentDetailVO;
import com.sanhuo.idms.vo.StudentPageVO;
import com.sanhuo.idms.vo.StudentVO;
import com.sanhuo.lib.ExceptionUtil;
import com.sanhuo.security.UserVo;
import com.sanhuo.security.entity.User;
import com.sanhuo.security.service.UserService;
import com.sanhuo.triild.Basic.BaseMapper;
import com.sanhuo.triild.Basic.BaseService;
import com.sanhuo.triild.annotation.MapperWire;
import com.sanhuo.triild.page.Page;
import com.sanhuo.triild.page.PageSearch;
import com.sanhuo.utils.basic.BeanUtil;
import com.sanhuo.utils.basic.CollectionUtil;
import com.sanhuo.utils.basic.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class StudentService extends BaseService<Student> {

    @MapperWire
    private StudentMapper studentMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private DormService dormService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private TungService tungService;

    @Override
    public BaseMapper<Student> getBaseMapper() {
        return studentMapper;
    }

    public List<Student> findAll(StudentPageBO pageBO) {
        return studentMapper.findAll(pageBO);
    }

    public Page<StudentPageVO> listAll(StudentPageBO pageBO, PageSearch pageSearch) {
        return studentMapper.listAll(pageBO, pageSearch);
    }

    public StudentDetailVO addDetailData(String id) throws IllegalAccessException {
        User user = userService.findById(id);
        Student student = studentMapper.findById(id);
        StudentDetailVO result = new StudentDetailVO();
        BeanUtil.copyBean(user, result);
        BeanUtil.copyBeanIgnore(student, result, "id");
        if (StringUtil.isNotBlank(student.getDormId())) {
            Dorm dorm = dormService.findById(student.getDormId());
            if (dorm != null) {
                Room room = roomService.findById(dorm.getRoomId());
                result.setRoomNum(room.getNum());
                Tung tung = tungService.findById(dorm.getTungId());
                result.setTungNum(tung.getNum());

            }
        }
//        if (StringUtil.isNotBlank(student.getCollegeId())) {
//            College college = collegeService.findById(student.getCollegeId());
//            if (college != null) {
//                result.setCollege(college.getName());
//            }
//        }
        return result;
    }

    public List<StudentPageVO> addDromAndCollegeData(List<Student> studentList) throws IllegalAccessException {
        List<StudentPageVO> result = new ArrayList<>();
        if (!CollectionUtil.isEmpty(studentList)) {
            for (Student student : studentList) {
                StudentPageVO studentVO = new StudentPageVO();
                BeanUtil.copyBean(student, studentVO);
                result.add(studentVO);
            }
        }
        return result;
    }

    public void adminUpdateStudent(final UserVo user, AdminUpdateStudentBO bo) throws IllegalAccessException {
        Student student = studentMapper.findById(bo.getId());
        ExceptionUtil.throwException(student == null, "不存在此学号:[" + bo.getId() + "]的学生");
        student.setBunkNum(bo.getBunkNum());
        if (StringUtil.isNotBlank(bo.getTungId()) && StringUtil.isNotBlank(bo.getRoomId())) {
            Dorm dorm = dormService.findByTungAndRoom(bo.getTungId(), bo.getRoomId());
            if (dorm == null) {
                Dorm newDorm = new Dorm();
                String dormId = StringUtil.UUID();
                newDorm.setId(dormId);
                BeanUtil.copyBeanIgnore(bo, newDorm, "id");
                newDorm.creatorAndUpdatorInfo(user);
                dormService.insert(newDorm);
                student.setDormId(dormId);
            } else {
                student.setDormId(dorm.getId());
            }
        }

        student.updatorInfo(user);
        studentMapper.update(student);
    }

    public void save(final UserVo user, StudentSaveBO bo) throws IllegalAccessException {
        User newUser = new User();
        BeanUtil.copyBean(bo, newUser);
        newUser.setRoleId("03");
        newUser.creatorInfo(user);
        userService.save(newUser);
        Student student = new Student();
        student.creatorAndUpdatorInfo(user);
        BeanUtil.copyBean(bo, student);
        save(student);
    }

    public List<Student> findByDormId(String dormId) {
        return studentMapper.findByDormId(dormId);
    }

    public StudentVO detail(String id) {
        return studentMapper.detail(id);
    }

}
